#include <sys_config.h>

#ifdef HILINK_SUPPORT
#include <types.h>
#include <osal/osal.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <api/libosd/osd_lib.h>
#include <api/libpub/lib_frontend.h>

#include "osdobjs_def.h"
#include "osd_config.h"
#include "string.id"
#include "images.id"
#include "menus_root.h"
#include "win_com.h"
#include "win_com_popup.h"

#include <hld/ali_3g_mdm/ali_3g_mdm_api.h>
#include <api/libhilink/lib_hilink.h>
#include "copper_common/com_api.h"

#include "win_net_choose.h"

#define WIN_SH_IDX	  WSTL_WIN_BODYRIGHT_01_HD
#define WIN_HL_IDX	  WSTL_WIN_BODYRIGHT_01_HD
#define WIN_SL_IDX	  WSTL_WIN_BODYRIGHT_01_HD
#define WIN_GRY_IDX  WSTL_WIN_BODYRIGHT_01_HD

#define CON_SH_IDX   WSTL_BUTTON_01_HD
#define CON_HL_IDX   WSTL_BUTTON_02_HD
#define CON_SL_IDX   WSTL_BUTTON_01_HD
#define CON_GRY_IDX  WSTL_BUTTON_07_HD

#define TXTN_SH_IDX   WSTL_BUTTON_01_FG_HD
#define TXTN_HL_IDX   WSTL_BUTTON_02_FG_HD
#define TXTN_SL_IDX   WSTL_BUTTON_01_FG_HD
#define TXTN_GRY_IDX  WSTL_BUTTON_07_HD


#ifndef SD_UI
#ifndef SUPPORT_CAS_A
/* define in win_com_menu_define.h already */
#define	W_L     	248
#define	W_T     	98
#define	W_W     	692
#define	W_H     	488
#endif

#define SCB_L 		(W_L + 10)
#define SCB_T 		110//150
#define SCB_W 		12//sharon 18
#define SCB_H 		460

#define CON_L		SCB_L//(SCB_L + SCB_W + 10)
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 72 - (SCB_W + 4))
#define CON_H		40
#define CON_GAP 	12

#define TXTN_L_OF	10
#define TXTN_T_OF  	0
#define TXTN_W		(CON_W-20)
#define TXTN_H     	40

#define ITEM_H          CON_H
#define LIST_ITEMCNT    4
#define ITEM_GAP	6//8

#define LST_L		(CON_L + 10)
#define LST_T		CON_T
#define LST_W		(W_W - 40)
#define LST_GAP 	4
#define LST_H		((ITEM_H+ITEM_GAP)*LIST_ITEMCNT+LST_GAP)

#define HILINK_LINE_L	(LST_L)
#define HILINK_LINE_T  	((LST_T + (ITEM_H+ITEM_GAP)*7 + LST_GAP) + 14)
#define HILINK_LINE_W	(LST_W-40)
#define HILINK_LINE_H   2

#else
#define	W_L     	105
#define	W_T     	57
#define	W_W     	482
#define	W_H     	370

#define SCB_L 		(W_L + 10)
#define SCB_T 		64//150
#define SCB_W 		12//sharon 18
#define SCB_H 		348

#define CON_L		SCB_L//(SCB_L + SCB_W + 10)
#define CON_T		(W_T + 6)
#define CON_W		(W_W - 34 - (SCB_W + 4))
#define CON_H		32//28//40
#define CON_GAP 	7

#define TXTN_L_OF	10
#define TXTN_T_OF  	4
#define TXTN_W		(CON_W-20)
#define TXTN_H     	24//40

#define ITEM_H          CON_H
#define LIST_ITEMCNT    4

#define LST_L		(CON_L + 10)
#define LST_T		CON_T
#define LST_W		(W_W - 40)
#define LST_GAP 	4
#define LST_H		((ITEM_H+ITEM_GAP)*LIST_ITEMCNT+LST_GAP)

#define HILINK_LINE_L	(LST_L)
#define HILINK_LINE_T  	((LST_T + (ITEM_H+ITEM_GAP)*7 + LST_GAP) + 14)
#define HILINK_LINE_W	(LST_W-40)
#define HILINK_LINE_H   2
#endif

extern CONTAINER g_win_hilink;

extern CONTAINER g_win_hilink_profile;
extern CONTAINER g_win_hilink_automode;
//extern CONTAINER g_win_hilink_traffic;


extern CONTAINER g_win_hilink_profile_con;
extern CONTAINER g_win_hilink_automode_con;
extern CONTAINER g_win_hilink_traffic_con;

//extern CONTAINER g_win_hilink_state_con;
extern CONTAINER hilink_connect_con;

extern TEXT_FIELD hilink_profile;
extern TEXT_FIELD hilink_automode;
extern TEXT_FIELD hilink_traffic;
extern TEXT_FIELD hilink_state;

extern TEXT_FIELD hilink_connect;

extern TEXT_FIELD hilink_line;


static PRESULT win_hilink_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);
static VACTION win_hilink_item_con_keymap(POBJECT_HEAD pObj, UINT32 key);

static VACTION win_hilink_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT win_hilink_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);


#define LDEF_CON(root, varCon,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,conobj,focusID)		\
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, CON_SH_IDX,CON_HL_IDX,CON_SL_IDX,CON_GRY_IDX,   \
    win_hilink_item_con_keymap,win_hilink_item_con_callback,  \
    conobj, ID,1)

    
#define LDEF_TXTNAME(root,varTxt,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, TXTN_SH_IDX,TXTN_HL_IDX,TXTN_SL_IDX,TXTN_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 10,0,resID,str)

#define LDEF_MM_ITEM(root,varCon,nxtObj,varTxtName,ID,IDu,IDd,l,t,w,h,nameID)	\
	LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,l,t,w,h,&varTxtName,1)	\
	LDEF_TXTNAME(&varCon,varTxtName,NULL,1,1,1,1,1,l + TXTN_L_OF, t + TXTN_T_OF,TXTN_W,TXTN_H,nameID,NULL)	
	
#define LDEF_HILINK_LINE(root, varTxt, nxtObj, l, t, w, h, str)		\
    DEF_TEXTFIELD(varTxt,&root,nxtObj,C_ATTR_ACTIVE,0, \
                  0, 0, 0, 0, 0, l, t, w, h, WSTL_LINE_MENU_HD,WSTL_LINE_MENU_HD,WSTL_LINE_MENU_HD,WSTL_LINE_MENU_HD,   \
                  NULL,NULL,  \
                  C_ALIGN_CENTER | C_ALIGN_VCENTER, 0,0,0,str)


LDEF_MM_ITEM(g_win_hilink,g_win_hilink_profile_con,&g_win_hilink_automode_con,hilink_profile,1,4,2,CON_L, CON_T + (CON_H + CON_GAP)*0   \
            ,CON_W,CON_H,RS_HILINK_PROFILE)

LDEF_MM_ITEM(g_win_hilink,g_win_hilink_automode_con,&g_win_hilink_traffic_con,hilink_automode,2,1,3,CON_L, CON_T + (CON_H + CON_GAP)*1   \
            ,CON_W,CON_H,RS_HILINK_AUTOMODE)

LDEF_MM_ITEM(g_win_hilink,g_win_hilink_traffic_con,&hilink_connect_con,hilink_traffic,3,2,4,CON_L, CON_T + (CON_H + CON_GAP)*2   \
            ,CON_W,CON_H,RS_HILINK_TRAFFIC)

LDEF_MM_ITEM(g_win_hilink,hilink_connect_con,&hilink_line,hilink_connect,4,3,1,CON_L, CON_T + (CON_H + CON_GAP)*3   \
            ,CON_W,CON_H,RS_COMMON_CONNECT)

LDEF_HILINK_LINE(g_win_hilink, hilink_line, &hilink_state, HILINK_LINE_L, HILINK_LINE_T, HILINK_LINE_W, HILINK_LINE_H, NULL)

LDEF_TXTNAME(&g_win_hilink, hilink_state, NULL, 0,0,0,0,0,HILINK_LINE_L + 10, HILINK_LINE_T + 10, HILINK_LINE_W, CON_H, 0,display_strs[0])

DEF_CONTAINER(g_win_hilink,NULL,NULL,C_ATTR_ACTIVE,0, \
    1,0,0,0,0, W_L,W_T,W_W,W_H, WIN_SH_IDX,WIN_HL_IDX,WIN_SL_IDX,WIN_GRY_IDX,\
    win_hilink_keymap,win_hilink_callback,  \
    (POBJECT_HEAD)&g_win_hilink_profile_con, 1,0);



enum{
    HILINK_PLUGIN = 0,
    HILINK_PLUGOUT,
};

extern HILINK_AUTOMODE_INFO_STRU hilink_automode_info;

static HILINK_CONNECTSTATE_INFO_STRU hilink_connect_info = {0};

static ID	hilink_connect_timer = OSAL_INVALID_ID;

static UINT8 plug_flag = 0;
static VACTION win_hilink_keymap(POBJECT_HEAD pObj, UINT32 key)
{

    VACTION ret= VACT_PASS;

    switch(key)
    {
        case V_KEY_EXIT:
        case V_KEY_MENU:
            ret = VACT_CLOSE;
            break;
        case V_KEY_UP:
            ret = VACT_CURSOR_UP;
            break;
        case V_KEY_DOWN:
            ret = VACT_CURSOR_DOWN;
            break;
        default:
        break;
    }
    return ret;
}

void win_hilink_init(void)
{
    char str1[50]="";
    int result= 0;

	remove_menu_item(&g_win_hilink, (OBJECT_HEAD *)&g_win_hilink_traffic_con, CON_H + CON_GAP);                
    result = hilink_getconnectstate_fromlocall(&hilink_connect_info);
    hilink_getautomodeinfo_fromserver(&hilink_automode_info);
    if(HILINK_SIMCARD_OFFLINE == hilink_connect_info.ulsimcardstate)
    {
        sprintf(str1, "state :  No SIM Card");

        //OSD_SetAttr(&hilink_connect_con, C_ATTR_INACTIVE);
        OSD_SetAttr(&hilink_connect, C_ATTR_INACTIVE);
    }
    else
    {

        if(HILINK_AUTOMODE_DISABLE == hilink_automode_info.ulautomode)
        {
            if((HILINK_GETSTATE_CONNECT == hilink_connect_info.ulconnectstate) ||
               (HILINK_GETSTATE_DISCONNECT == hilink_connect_info.ulconnectstate)) 
            {
                //OSD_SetAttr(&hilink_connect_con, C_ATTR_ACTIVE);
                OSD_SetAttr(&hilink_connect, C_ATTR_ACTIVE);
            }
        }
        else if(HILINK_AUTOMODE_ENABLE == hilink_automode_info.ulautomode)
        {
            //OSD_SetAttr(&hilink_connect_con, C_ATTR_INACTIVE);
            OSD_SetAttr(&hilink_connect, C_ATTR_INACTIVE);
        }

        if(HILINK_GETSTATE_CONNECTING == hilink_connect_info.ulconnectstate)
        {
            sprintf(str1, "state :  connecting...");
        }
        else if(HILINK_GETSTATE_CONNECT == hilink_connect_info.ulconnectstate)
        {
            sprintf(str1, "state :  connected");
            OSD_SetTextFieldContent(&hilink_connect, STRING_ID, RS_COMMON_DISCONNECT);
        }
        else if(HILINK_GETSTATE_DISCONNECT == hilink_connect_info.ulconnectstate)
        {
            sprintf(str1, "state :  disconnected");
            OSD_SetTextFieldContent(&hilink_connect, STRING_ID, RS_COMMON_CONNECT);
        }
        else if(HILINK_GETSTATE_DISCONNECTING == hilink_connect_info.ulconnectstate)
        {
            sprintf(str1, "state :  disconnecting...");
        }
    }

    OSD_SetTextFieldContent(&hilink_state, STRING_ANSI, (UINT32)str1);
}

static PRESULT win_hilink_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
    PRESULT ret = PROC_PASS;
	SYSTEM_DATA* sys_data;
    char str1[50]="";

	sys_data = sys_data_get();	
    
    switch(event)
    {
        case EVN_PRE_OPEN:
            if(1 == plug_flag)
            {
                win_hilink_init();
            }
	     	wincom_open_title(pObj,RS_TOOL_HILINK, 0);        
            break;
        case EVN_POST_OPEN:
            break;
        case EVN_PRE_CLOSE:
            *((UINT32*)param2) &= ~C_CLOSE_CLRBACK_FLG;	
            break;
        case EVN_POST_CLOSE:    
            break;
        case EVN_MSG_GOT:
            if((CTRL_MSG_SUBTYPE_CMD_EXIT == param1) || (CTRL_MSG_SUBTYPE_CMD_EXIT_ALL == param1))
            {
                ret = PROC_LEAVE;
            }    
            if(CTRL_MSG_SUBTYPE_CMD_HILINK == param1)
            {
                switch(param2)
                {
                    case HILINK_STATECHANGE_NOCARD :
                        sprintf(str1, "state :  No SIM Card");
                        //OSD_SetAttr(&hilink_connect_con, C_ATTR_INACTIVE);
                        OSD_SetAttr(&hilink_connect, C_ATTR_INACTIVE);
                        OSD_SetTextFieldContent(&hilink_state, STRING_ANSI, (UINT32)str1);
                        break;
                    case HILINK_STATECHANGE_CONNECTING :
                        sprintf(str1, "state :  connecting...");
                        //OSD_SetAttr(&hilink_connect_con, C_ATTR_INACTIVE);
                        OSD_SetAttr(&hilink_connect, C_ATTR_INACTIVE);
                        OSD_SetTextFieldContent(&hilink_state, STRING_ANSI, (UINT32)str1);
                        break;
                    case HILINK_STATECHANGE_CONNECT :
                        sprintf(str1, "state :  connected");
                        //OSD_SetAttr(&hilink_connect_con, C_ATTR_ACTIVE);
                        OSD_SetAttr(&hilink_connect, C_ATTR_ACTIVE);
                        OSD_SetTextFieldContent(&hilink_state, STRING_ANSI, (UINT32)str1);
                        OSD_SetTextFieldContent(&hilink_connect, STRING_ID, RS_COMMON_DISCONNECT);
                        break;
                    case HILINK_STATECHANGE_DISCONNECT :
                        sprintf(str1, "state :  disconnected");
                        //OSD_SetAttr(&hilink_connect_con, C_ATTR_ACTIVE);
                        OSD_SetAttr(&hilink_connect, C_ATTR_ACTIVE);
                        OSD_SetTextFieldContent(&hilink_state, STRING_ANSI, (UINT32)str1);
                        OSD_SetTextFieldContent(&hilink_connect, STRING_ID, RS_COMMON_CONNECT);
                        break;
                    case HILINK_STATECHANGE_DISCONNECTING :
                        sprintf(str1, "state :  disconnecting....");
                        OSD_SetTextFieldContent(&hilink_state, STRING_ANSI, (UINT32)str1);
                        //OSD_SetAttr(&hilink_connect_con, C_ATTR_INACTIVE);
                        OSD_SetAttr(&hilink_connect, C_ATTR_INACTIVE);
                      break;
                    case HILINK_STATUS_PLUGOUT:
                        ret = PROC_LEAVE;
                        return ret;
                        break;
                    default :
                        break;
                } 
                if(HILINK_AUTOMODE_ENABLE == hilink_automode_info.ulautomode)
                {
                    OSD_SetAttr(&hilink_connect, C_ATTR_INACTIVE);
                }
                OSD_TrackObject((POBJECT_HEAD)&g_win_hilink, C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);
            }
            /*
            if ((param1 == CTRL_MSG_SUBTYPE_CMD_HILINK)&&(param2 == HILINK_STATUS_CHANGE))
            {
                win_hilink_init();
                OSD_TrackObject((POBJECT_HEAD)&g_win_hilink, C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);
            }
            */
            break;
        default:
            break;
    }
    return ret;    
}

static VACTION win_hilink_item_con_keymap(POBJECT_HEAD pObj, UINT32 key)
{
    VACTION ret= VACT_PASS;

    switch(key)
    {
        case V_KEY_ENTER:
            ret = VACT_ENTER;
            break;
        default:
            break;
    }
    return ret;
}

#if 1
static HILINK_SETCONNECT_STATE_STRU hilink_set_connect = {0};
void win_hilink_connect_proc()
{
    char str1[50]="";
    int result= 0;

    if(RS_COMMON_CONNECT == hilink_connect.wStringID)
    {
        sprintf(str1, "state :  connecting...");
    
        hilink_set_connect.ulsetconnectstate = HILINK_CONNECTSTATE_SETCONNECT;
    }
    else
    {
        sprintf(str1, "state :  disconnecting...");
  
        hilink_set_connect.ulsetconnectstate = HILINK_CONNECTSTATE_SETDISCONNECT;
    }
    //OSD_SetAttr(&hilink_connect_con, C_ATTR_INACTIVE);
    OSD_SetAttr(&hilink_connect, C_ATTR_INACTIVE);
    //g_win_hilink.FocusObjectID = 1;
    OSD_SetTextFieldContent(&hilink_state, STRING_ANSI, (UINT32)str1);  
    OSD_TrackObject((POBJECT_HEAD)&g_win_hilink, C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);
    
    hilink_setconnectstate_toserver(hilink_set_connect);
}
#else
void win_hilink_connect_proc()
{
    char str1[50]="";
    int result= 0;
                


    if(RS_COMMON_CONNECT == hilink_connect.wStringID)
    {
        sprintf(str1, "state :  connecting...");
    
        hilink_connect_info.ulconnectstate = HILINK_STATE_CONNECT;
    }
    else
    {
        sprintf(str1, "state :  disconnecting...");
  
        hilink_connect_info.ulconnectstate = HILINK_STATE_DISCONNECT;
    }
    OSD_SetAttr(&hilink_connect_con, C_ATTR_INACTIVE);
    OSD_SetAttr(&hilink_connect, C_ATTR_INACTIVE);
    g_win_hilink.FocusObjectID = 1;
    OSD_SetTextFieldContent(&hilink_state, STRING_ANSI, (UINT32)str1);  
    OSD_TrackObject((POBJECT_HEAD)&g_win_hilink, C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);
    
    hilink_setconnectstate_toserver(hilink_connect_info);
    
    
    int i = 0;
    while(i = 10)
    {
        osal_task_sleep(500);
        
        hilink_getconnectstate_fromserver(&hilink_connect_info);

        if(RS_COMMON_CONNECT == hilink_connect.wStringID)
        {
            if(HILINK_CONNECTSTATE_OK == hilink_connect_info.ulconnectstate)
                break;
        }
        else if(RS_COMMON_DISCONNECT == hilink_connect.wStringID)
        {
            if(902 == hilink_connect_info.ulconnectstate)
                break;
        }
    }
    win_hilink_init();
    g_win_hilink.FocusObjectID = 4;
    OSD_TrackObject((POBJECT_HEAD)&g_win_hilink, C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);
}
#endif
static PRESULT win_hilink_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
    PRESULT ret = PROC_PASS;
    UINT32  param = 0;
    PCONTAINER win_hilink = &g_win_hilink;

    UINT8 focus_id = win_hilink->FocusObjectID;
    
    param = MENU_OPEN_TYPE_MENU;
    switch(event)
    {
    case EVN_UNKNOWN_ACTION:
        if(VACT_ENTER == (VACTION)(param1>>16))
        {
             switch(focus_id)
             {
                case 1:
                    if(OSD_ObjOpen((POBJECT_HEAD)(&g_win_hilink_profile), param) != PROC_LEAVE)
                    {
        		        menu_stack_push((POBJECT_HEAD)(&g_win_hilink_profile));
                    }
                    break;
                case 2:
                    if(OSD_ObjOpen((POBJECT_HEAD)(&g_win_hilink_automode), param) != PROC_LEAVE)
                    {
        		        menu_stack_push((POBJECT_HEAD)(&g_win_hilink_automode));
                    }                    
                    break;
                    #if 0
                case 3:
                    if(OSD_ObjOpen((POBJECT_HEAD)(&g_win_hilink_traffic), param) != PROC_LEAVE)
                    {
        		        menu_stack_push((POBJECT_HEAD)(&g_win_hilink_traffic));
                    }                    
                    break;  
                    #endif
                case 4:
                    if(C_ATTR_ACTIVE == hilink_connect.head.bAttr)
                    {
                        win_hilink_connect_proc();
                    }
                    break;
                default :
                    break;
             }
        }
        break;
    default:
        break;
    }
    return ret;    
}

int hilink_plug_callback(unsigned int ulevent,void *pvhilinkdev)
{
    int slret = 0;
    
    if ((USBNET_DEVICE_PLUG_OUT < ulevent)||(NULL == pvhilinkdev))
    {
        HILINK_PRINT("%s:%d:error.\r\n",__FUNCTION__,__LINE__);
        return HILINK_PARA_ERR;
    }

    switch(ulevent)
    {
        case USBNET_DEVICE_PLUG_IN:
        {
            ap_send_msg(CTRL_MSG_SUBTYPE_HILINK_PLUGIN, (unsigned int)(pvhilinkdev), FALSE);
        }
        break;
        
        case USBNET_DEVICE_PLUG_OUT:
        {
            ap_send_msg(CTRL_MSG_SUBTYPE_HILINK_PLUGOUT, (unsigned int)(pvhilinkdev), FALSE);
            //libc_printf("plug out\n");
        }
        break;

        default:
        {
            HILINK_PRINT("%s:%d:error.\r\n",__FUNCTION__,__LINE__);
            return HILINK_PARA_ERR;
        }
    }

    return HILINK_OK;
}

static void hilink_show_hint(UINT32 dev_id, UINT32 hint_type)
{

	char hint[64];
    UINT8 back_saved;
    sprintf(hint, "Hilink ");

    switch(dev_id)
    {
        case 0:
            break;

        case 1:
            break;

        default :
            return;
    }
    
	switch (hint_type)
	{
		case HILINK_PLUGIN:
			strcat(hint, "connected !");
			break;
		case HILINK_PLUGOUT:
			strcat(hint, "disconnected !");
			break;
		default:
			return;
	}

	win_compopup_init(WIN_POPUP_TYPE_SMSG);
	win_compopup_set_msg_ext(hint, NULL, 0);
    win_compopup_open_ext(&back_saved);	
	osal_task_sleep(1000);

    win_compopup_smsg_restoreback();
}

int ap_hilink_message_proc(unsigned int ulmsgtype, unsigned int ulmsgcode)
{
    int slret = 0;
    POBJECT_HEAD    menu = menu_stack_get_top();

    UINT32 netdevicetype = 0;
    UINT32 net_choose_flag = 0;

    if ((CTRL_MSG_SUBTYPE_HILINK_PLUGIN > ulmsgtype)||(CTRL_MSG_SUBTYPE_HILINK_STATECHANGE < ulmsgtype)
        /*||(0 == ulmsgcode)*/)
    {
        HILINK_PRINT("%s:%d:error.\r\n",__FUNCTION__,__LINE__);
        return HILINK_PARA_ERR;
    }

    if (CTRL_MSG_SUBTYPE_HILINK_PLUGIN == ulmsgtype)
    {
        (void)hilink_plug_setflag(HILINK_EVENT_PLUGIN);
        (void)hilink_creat_task(ulmsgcode);
          // to do OSD
        hilink_show_hint(0 ,HILINK_PLUGIN);
    	// auto reflash menu!
    	if(((POBJECT_HEAD)&g_win_mainmenu == menu) || ((POBJECT_HEAD)&g_win_submenu == menu))
    	{
            //if(FALSE== ap_get_key_task_get_key_flag())
    		    OSD_ObjOpen(menu, 0);					
    	}	
        ap_send_msg(CTRL_MSG_SUBTYPE_CMD_HILINK, HILINK_STATUS_PLUGIN, FALSE);
        net_choose_card_choose_flag[NET_CHOOSE_HILINK_ID] = 1;
        plug_flag = 1;

        net_choose_flag = sys_data_get_net_choose_flag();
        nic_getcur_netdevice(&netdevicetype);
        if(NET_CHOOSE_HILINK_ID == net_choose_flag)
        {
        	nic_switch_netdevice(netdevicetype,net_choose_flag);
        }
    }
    else if (CTRL_MSG_SUBTYPE_HILINK_PLUGOUT == ulmsgtype) 
    {
        (void)hilink_plug_setflag(HILINK_EVENT_PLUGOUT);

        // to do OSD
        hilink_show_hint(0 ,HILINK_PLUGOUT);
        // auto reflash menu!
        if(((POBJECT_HEAD)&g_win_mainmenu == menu) || ((POBJECT_HEAD)&g_win_submenu == menu))
        {
            //if(FALSE== ap_get_key_task_get_key_flag())
        	    OSD_ObjOpen(menu, 0);					
        }
        ap_send_msg(CTRL_MSG_SUBTYPE_CMD_HILINK, HILINK_STATUS_PLUGOUT, FALSE);
        net_choose_card_choose_flag[NET_CHOOSE_HILINK_ID] = 0;
        plug_flag = 0;
    }
    else
    {
        #if 0
        if(HILINK_STATECHANGE_INITOK == ulmsgcode)
        {               
            // to do OSD
            hilink_show_hint(0 ,HILINK_PLUGIN);
        	// auto reflash menu!
        	if(((POBJECT_HEAD)&g_win_mainmenu == menu) || ((POBJECT_HEAD)&g_win_submenu == menu))
        	{
                //if(FALSE== ap_get_key_task_get_key_flag())
        		    OSD_ObjOpen(menu, 0);					
        	}	
            ap_send_msg(CTRL_MSG_SUBTYPE_CMD_HILINK, HILINK_STATUS_PLUGIN, FALSE);
            net_choose_card_choose_flag[NET_CHOOSE_HILINK_ID] = 1;
            plug_flag = 1;
        }
        else
        {
            ap_send_msg(CTRL_MSG_SUBTYPE_CMD_HILINK, ulmsgcode, FALSE);
        }
        #endif
        ap_send_msg(CTRL_MSG_SUBTYPE_CMD_HILINK, ulmsgcode, FALSE);
    }    
    return HILINK_OK;
}

int hilink_sendmessage_statechange(unsigned int ulstatechange)
{
    //HILINK_PRINT("%s:%d.\r\n",__FUNCTION__,ulstatechange);
    ap_send_msg(CTRL_MSG_SUBTYPE_HILINK_STATECHANGE,ulstatechange, FALSE);
    return HILINK_OK;
}

int hilink_attach(void)
{
    ali_usbnet_dev_register_notify(hilink_plug_callback);
    return HILINK_OK;
}

#endif
